/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import type.UserTitle;

/**
 *
 * @author Andy
 */
@Entity(name="Person")
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public abstract class PersonEntity extends DataObjectEntity implements Serializable {
    
    private String name;
    private String password;
    
    private String firstName;
    private String lastName;
    private UserTitle title;
    private boolean locked;

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

 
    @OneToOne(mappedBy = "person", cascade = {CascadeType.ALL})
    private ContactEntity contact = new ContactEntity();
    
    @ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
    
    @ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private Collection<RightEntity> rights = new ArrayList<RightEntity>();

    public PersonEntity() {
        super();
    }

    public PersonEntity(String firstName, String lastName, UserTitle title) {
        this();
        this.firstName = firstName;
        this.lastName = lastName;
        this.title = title;       
    }

    public PersonEntity(String name, String password, String firstName, String lastName, UserTitle title) {
        this(firstName, lastName, title);
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }
//
    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public UserTitle getTitle() {
        return title;
    }

    public void setTitle(UserTitle title) {
        this.title = title;
    }

    public ContactEntity getContact() {
        return contact;
    }

    public void setContact(ContactEntity contact) {
        this.contact = contact;
    }

    public Collection<RoleEntity> getRoles() {
        return roles;
    }

    public void setRoles(Collection<RoleEntity> roles) {
        this.roles = roles;
    }

    public Collection<RightEntity> getRights() {
        return rights;
    }

    public void setRights(Collection<RightEntity> rights) {
       
         this.rights=rights;
    }
    
    public String getFullname() {
        return getFirstName() + " " + getLastName();
    }

    @Override
    public String childToString() {
        String res = "PersonEntity{" + "name=" + name + ", password=" + password + ", firstName=" + firstName + ", lastName=" + lastName + ", title=" + title + ", locked=" + locked + ", contact=" + contact + ", roles=" + roles + ", rights=" + rights + '}';
        if(this instanceof CustomerEntity )
            res += "\n" + ((CustomerEntity)this).rootToString();
        else if(this instanceof SalesLeadEntity)
            res += "\n" + ((SalesLeadEntity)this).rootToString();
        else if(this instanceof SystemUserEntity)
            res += "\n" + ((SystemUserEntity)this).rootToString();
        else if(this instanceof VendorEntity)
            res += "\n" + ((VendorEntity)this).rootToString();
        return res;
    }   
    
}
